package com.spzx.product.service.impl;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.spzx.product.api.domain.ProductSkuVo;
import com.spzx.product.api.domain.SkuPriceVo;
import com.spzx.product.domain.Brand;
import com.spzx.product.domain.Product;
import com.spzx.product.api.domain.ProductSku;
import com.spzx.product.domain.vo.*;
import com.spzx.product.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.function.Consumer;
import java.util.function.Supplier;

@Service
public class ChannelServiceImpl implements IChannelService {
    @Autowired
    private ICategoryService categoryService;
    @Autowired
    private IProductSkuService productSkuService;
    @Autowired
    private IBrandService brandService;
    @Autowired
    private ISkuStockService skuStockService;
    @Autowired
    private IProductService productService;
    @Autowired
    private IProductDetailsService productDetailsService;
    @Autowired
    private ThreadPoolExecutor threadPoolExecutor;

    @Override
    public IndexDataVo index() {
        List<CategoryVo> levelOneCategory = categoryService.getLevelOneCategory();
        List<ProductSkuVo> topSale = productSkuService.getTopSale();
        IndexDataVo indexDataVo = new IndexDataVo();
        indexDataVo.setCategoryList(levelOneCategory);
        indexDataVo.setProductSkuList(topSale);
        return indexDataVo;
    }

    @Override
    public List<CategoryVo> tree() {
        return categoryService.tree();
    }

    @Override
    public List<ProductSkuVo> selectProductSkuList(SkuQuery skuQuery) {
        return productSkuService.selectProductSkuList(skuQuery);
    }

    @Override
    public List<Brand> list() {
        return brandService.list();
    }

    @Override
    public ItemVo item(Long skuId) {
        ItemVo itemVo = new ItemVo();
        //获取sku信息
        CompletableFuture<ProductSku> futureProductSku = CompletableFuture.supplyAsync(new Supplier<ProductSku>() {
            @Override
            public ProductSku get() {
                ProductSku productSku = productSkuService.getProductSku(skuId);
                itemVo.setProductSku(productSku);
                return productSku;
            }
        }, threadPoolExecutor);

        ///获取商品最新价格
        CompletableFuture<Void> futureSkuPrice = CompletableFuture.runAsync(new Runnable() {
            @Override
            public void run() {
                SkuPriceVo skuPrice = productSkuService.getSkuPrice(skuId);
                itemVo.setSkuPrice(skuPrice);
            }
        }, threadPoolExecutor);

        //获取商品库存
        CompletableFuture<Void> futureSkuStock = CompletableFuture.runAsync(() -> {
            SkuStockVo skuStock = skuStockService.getSkuStock(skuId);
            itemVo.setSkuStock(skuStock);
        }, threadPoolExecutor);


        CompletableFuture<Void> futureProduct = futureProductSku.thenAcceptAsync(new Consumer<ProductSku>() {
            @Override
            public void accept(ProductSku productSku) {
                //获取商品id
                Long productId = productSku.getProductId();
                //获取商品信息
                Product product = productService.getById(productId);
                //获取商品轮播图列表
                String[] sliderUrlList = product.getSliderUrls().split(",");
                itemVo.setSliderUrlList(sliderUrlList);
                //获取商品规格信息
                JSONArray specValueList = JSON.parseArray(product.getSpecValue());
                itemVo.setSpecValueList(specValueList);
            }
        }, threadPoolExecutor);

        //获取商品详情列表图片
        CompletableFuture<Void> futureProductDetails = futureProductSku.thenAcceptAsync(productSku -> {
            //获取商品id
            Long productId = productSku.getProductId();
            String[] detailsImageUrlList = productDetailsService.getProductDetails(productId);
            itemVo.setDetailsImageUrlList(detailsImageUrlList);
        }, threadPoolExecutor);

        //商品规格对应商品skuId信息
        CompletableFuture<Void> futureSkuSpecValue = futureProductSku.thenAcceptAsync(productSku -> {
            //获取商品id
            Long productId = productSku.getProductId();
            Map<String, Long> skuSpecValueMap = productSkuService.getSkuSpecValue(productId);
            itemVo.setSkuSpecValueMap(skuSpecValueMap);
        });

        //等待所有任务都完成
        CompletableFuture.allOf(
                        futureProductSku,
                        futureProductDetails,
                        futureSkuPrice,
                        futureProduct,
                        futureSkuStock,
                        futureSkuSpecValue
                )//这是一个阻塞操作，程序会在此等待直到所有任务都完成。
                .join();
        //返回商品详情
        return itemVo;
    }
}
